<?php
	class MemView{
		
		private $crypt_var; 
		private $messages;
		function __construct($crypt_var = 'key'){
			$this->crypt_var = $crypt_var;
		}
		
		
		function __call($method, $params){
			
			
			switch ($method){
				case 'add_msg_error': $this->add_msg($params[0], 'error'); break;
				case 'add_msg_sucess': $this->add_msg($params[0], 'sucess'); break;
				case 'add_msg_warning': $this->add_msg($params[0], 'warning'); break;
				case 'redirect':
					if(count($params) == 1)
						$this->redirect_simple($params[0]);
					elseif (count($params)== 2)
						$this->redirect_complete('index.php', $params[0], $params[1]);
					elseif (count($params == 3))
						$this->redirect_complete('index.php', $params[0], $params[1], $params[2]);
				case 'ctrl_redirect':
					if(count($params) == 1)
						$this->redirect_simple($params[0]);
					elseif (count($params)== 2)
						$this->redirect_complete('ctrl.php', $params[0], $params[1]);
					elseif (count($params == 3))
						$this->redirect_complete('ctrl.php', $params[0], $params[1], $params[2]);
			}
			
		}
		
		public function add_msg($msg, $type){
			$_SESSION['msg_'.$this->crypt_var][$type][count($_SESSION['msg_'.$this->crypt_var][$type])] = $msg;
		}
		
		public function messages(){
			$return = '';
			$errors = $_SESSION['msg_'.$this->crypt_var]['error'];
			$sucesss = $_SESSION['msg_'.$this->crypt_var]['sucess'];
			$warnings = $_SESSION['msg_'.$this->crypt_var]['warning'];
			unset($_SESSION['msg_'.$this->crypt_var]);
			if(count($errors) > 0){
				$return .= '<div id="div_error"><ul>';
				for($m=0;count($errors)>$m;$m++)
					$return .= '<li>'.$errors[$m].'</li>';
				$return .= '</ul></div>';
			}
			if(count($sucesss) > 0){
				$return .= '<div id="div_sucesss"><ul>';
				for($m=0;count($sucesss)>$m;$m++)
					$return .= '<li>'.$sucesss[$m].'</li>';
				$return .= '</ul></div>';
			}
			if(count($warnings) > 0){
				$return .= '<div id="div_warnings"><ul>';
				for($m=0;count($warnings)>$m;$m++)
					$return .= '<li>'.$warnings[$m].'</li>';
				$return .= '</ul></div>';
			}
			return $return;
		}
		
		private function get_attributes($params){
			if(count($params)> 0){
		    	$key = array_keys($params);
		    	for($i=0;count($params)>$i;$i++)
		   			$attibutes .= ' '.$key[$i].'="'.$params[$key[$i]].'" ';				
			}
	    	return $attibutes;
		}
		
		
		// enconde url
		public function encode($url)
		{
			$pos_debut=strpos($url,"?");
		    if(!$pos_debut)	$sep="&";
		    $pos_fin = strlen($url);
		    if($pos_fin){
				$pos_long=$pos_fin-$pos_debut-1;
				$fin=substr($url,$pos_fin);
		    }else{
				$pos_long=strlen($url)-$pos_debut-1;
		    }
		    $debut=substr($url,0,$pos_debut+1);
		    $param=substr($url,$pos_debut+1,$pos_long);
		    $code = base64_encode($param);
		    return $debut.$sep.$this->crypt_var."=".$code.$fin;
		}
		
		public function ctrl_encode($ctrl, $action, $params = array()){
			$url = 'ctrl.php?'.$ctrl.'='.$action;
			$param = '';
			if(count($params) > 0){
				$keys = array_keys($params);
				for($i=0;count($keys)>$i;$i++){
					$param .= '&'.$keys[$i].'='.$params[$keys[$i]];
				}
			}
			return $this->encode($url.$param);
		}
		
		public function view_encode($ctrl, $action, $params = array()){
			$url = 'index.php?'.$ctrl.'='.$action;
			$param = '';
			if(count($params) > 0){
				$keys = array_keys($params);
				for($i=0;count($keys)>$i;$i++){
					$param .= '&'.$keys[$i].'='.$params[$keys[$i]];
				}
			}
			return $this->encode($url.$param);
		}
		//--------------------------------------------------------------------------------------------------------------------
		//Funcao que faz descriptografia, recebe uma string criptografada e retorna um array de valores
		// Recebe GH2J7Gv2g e retorna array('cod' => '1', 'id' => '123')
		public function decode($data)
		{
		    if($data)
		    {
			$decode_url=base64_decode($data);
			parse_str($decode_url, $tbl);
			$this->tbl = $tbl;
		        return($tbl);
		    }
		}
		
		
		public function redirect_simple($url){
			$url = $this->encode($url);
			header("Location: ".$url);
		}
		
		
		public function redirect_complete($file, $ctrl, $action, $params = array()){
			$url = $file.'?'.$ctrl.'='.$action;
			$param = '';
			if(count($params) > 0){
				$keys = array_key($params);
				for($i=0;count($keys)<$i;$i++){
					$param .= '&'.$keys[$i].'='.$params[$keys[$i]];
				}
			}
			header("Location: ".$url.$param);
		}

		
		public function getFileRoot(){
			return substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/')+1, strlen($_SERVER['PHP_SELF'])-strrpos($_SERVER['PHP_SELF'], '/')-1);
		}
		// create mensagem
		
		// call (aviso, sucesso, erro)
		
		// mostrar mensagem
		
		public function show_array($array){
			echo '<pre>';
			print_r($array);
			echo '</pre>';
		}
		
		public function link_view_encode(){
			$params = func_get_args();
			// 0 - Value
			// 1 - control
			// 2 - action
			// 3 - params link
			// 4 - attrubutes
			return '<a href="'.$this->view_encode($params[1], $params[2], $params[3]).'" '.$this->get_attributes($params[4]).'>'.$params[0].'</a>';
		}
		public function link_ctrl_encode(){
			$params = func_get_args();
			// 0 - Value
			// 1 - control
			// 2 - action
			// 3 - params link
			// 4 - attrubutes
			return '<a href="'.$this->ctrl_encode($params[1], $params[2], $params[3]).'" '.$this->get_attributes($params[4]).'>'.$params[0].'</a>';
		}
		
	}
?>